<?xml version="1.0" encoding="UTF-8"?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ja" lang="ja"
      xmlns:xul="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<head>
  <title>Ubiquity Web Search Commands for Japanese, nora - rakuten</title>
</head>
<body>
<p>This xhtml source contains commands for searching the Web.  Its
corresponding feed is <a href="ubiquity-commands-ja-nora-rakuten.html">here</a>.</p>


<div id="rakuten" style="display: none;">
    <style>
        body{overflow-x:hidden;}
        .itemkey{font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 0px 2px; margin-left:1px; max-width:20px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#333333;text-decoration: underline;vertical-align:text-top;}
        .itembreak{clear:both;display:block;height:0;visibility:hidden;}
        .pagenav{font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 1px 2px; max-width:30px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#000000;box-shadow:2px 2px 2px black;}
        .affi{font-size:xx-small;border: 1px solid #440000; -moz-border-radius:2px; padding:0px 2px 0px 2px; overflow:visible;float:none;background:#eecccc;color:#662222;}
    </style>
    <b>${query}</b>で 楽天を検索: ${resultsnum}件 ${options}
    <div class="items">
        {for item in items}
            <div class="item" id="item${item.key}" style="clear: both;border:1px solid #777777;-moz-border-radius:4px;background:#666666;margin:3px 0px 0px 0px;padding:0px;font-size:80%;">
                <div style="float:left;margin:2px 5px 2px 2px;min-height:64px;" class="thumbnail" id="thumbnail${item.key}"><img src="${item.image}" /></div>
                <div class="itemtitle" id="itemtitle${item.key}">
                    <a href="${item.url}" accesskey="${item.key}">
                        <span class="itemkey">${item.key}</span>
                        <span style="font-weight:bold;font-size:${item.itemnamesize}%;" class="itemname">${item.itemname}</span>
                    </a>
                </div>
                <div style="font-size:90%;" class="itemsub" id="itemsub${item.key}">
                    価格：<span style="font-weight:bold;font-size:110%;color:#ff3333">${item.price}</span>円
                    レビュー:${item.reviewCount}件 評価:${item.reviewAverage} 店舗:<a href="${item.shopUrl}">${item.shopName}</a>{if item.excludeTax} 税別{/if}{if !item.excludeShipping} 送料込{/if}{if item.canUseCreditCard} <img src="http://image.www.rakuten.co.jp/com/img/icon/cir_credit_ok.gif" height="10" />{/if}
                </div>
                <div class="itembreak" id="itembreak${item.key}">.</div>
            </div>
            <div class="itembreak">.</div>
        {forelse}
            <b>該当項目が見つかりませんでした</b>
        {/for}
        {for genre in genres}
            <div class="item" id="item${genre.key}" style="clear: both;border:1px solid #777777;-moz-border-radius:4px;background:#666666;margin:3px 0px 0px 0px;padding:0px;font-size:80%;">
                <a href="${genre.url}" accesskey="${genre.key}">
                    <span class="itemkey">${genre.key}</span>
                    <span style="font-weight:bold;" class="itemname">${genre.name}</span>
                    (${genre.itemCount})
                </a>
            </div>
        {/for}
    </div>
    <div class="bottom" style="position:absolute;bottom:11px;right:0px;height:13px;width:100%;padding:0px;margin:0px;background:rgba(40,40,40,0.7);">
        <div class="note" style="font-size:70%;clear:both;float:left;">※[Ctrl]+[Alt]+[番号] でジャンプ</div>
        <div class="navigation" style="font-size:70%;float:right;margin: 0px 5px 0px 0px;">
            <a id="prev" accesskey="8"><span class="pagenav">&lt;Prev(<u>8</u>)</span></a>
            Page <span id="page">${page}</span>
            <a id="next" accesskey="9"><span class="pagenav">Next(<u>9</u>)&gt;</span></a>
        </div>
    </div>
</div>


<script class="commands"><![CDATA[


noun_type_order = new CmdUtils.NounType("sort", ["price", "-price", "date", "-date"]);
noun_type_affi = new CmdUtils.NounType("affiliateflag", ["yes", "no", "defaultyes", "defaultno"]);


CmdUtils.CreateCommand({
    name: "rakuten",
    description: "楽天市場検索",
    takes: {search: noun_arb_text},
    modifiers:  {
                    orderby: noun_type_order, 
                    affi: noun_type_affi
                },
    icon: "http://www.rakuten.co.jp/favicon.ico",
	homepage: "http://code.google.com/p/ubiquity-commands-ja-nora/",
	author: {name: "hokunarin", email: "hokunarin@gmail.com"},
	license: "MPL",
    
    preview: function( previewBlock, directObject, mods ) {
        
        
        var ubiquityVersion = Application.prefs.get("extensions.ubiquity.lastversion").value;
        
        
        var prefObj = Application.prefs.get("extensions.hokunarin.jgoogle.animation");
        var ANIM = true;
        if(prefObj!=null){
            ANIM = eval(prefObj.value);
        }
        
        
        var noaffi = true;  //true でアフィリエイト無効、 false でアフィリエイト有効（Default値）
        
        var affiSettingObj = Application.prefs.get("extensions.hokunarin.rakuten.affiliate")
        if(affiSettingObj){
            noaffi = !eval(affiSettingObj.value);
        }
        
        var affiText = mods["affi"].text;
        
        if(affiText && affiText.length > 1){
            if(affiText=="no"){
                noaffi = true;
            }else if(affiText == "yes"){
                noaffi = false;
            }
        }
        
        affiliateId = noaffi ? "" : affiliateId;
        
        
        var developerId = "4f4df0b2545ad8fa5f76c163a8c954c9";
        var affiliateId = "0a4975b0.65c86202.0a4975cd.816e79f5";
        var affiliateUrlPrefix = "http://pt.afl.rakuten.co.jp/c/0a49763a.0293c8d9/?url=";
        
        var apiurl = "http://api.rakuten.co.jp/rws/2.0/json";
        
        var searchText = jQuery.trim(directObject.text);
        if(searchText.length < 1) {
            var previewStr = "楽天市場を検索します<br/><br/><b>rakuten キーワード</b><br/><br/>";
            previewStr += '<div style="font-size:85%;margin-left:20px;">';
            previewStr += '[Ctrl]+[Alt]+[番号] で各検索結果をブラウザに表示します<br/>';
            previewStr += '検索結果のプレビュー表示では <span class="pagenav" style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 1px 2px; max-width:30px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#000000;box-shadow:2px 2px 2px black;">&lt;Prev(<u>8</u>)</span> <span class="pagenav" style="font-size:xx-small; border-width:1px; border-style:solid;-moz-border-radius:2px; padding:0px 2px 1px 2px; max-width:30px; max-height:10px; overflow:visible;float:none;background: #dddddd;color:#000000;box-shadow:2px 2px 2px black;">Next(<u>9</u>)&gt;</span> のリンク、または [Ctrl]+[Alt]+[8], [Ctrl]+[Alt]+[9] のショートカットでページ送りが出来ます<br/><br/><br/>';
            previewStr += 'アフィリエイトを含むリンクについて<br/>';
            previewStr += '<b>rakuten キーワード affi yes</b> でアフィリエイトを含むリンクが表示されます<br/>';
            previewStr += '<b>rakuten キーワード affi no</b> でアフィリエイトは無効になります<br/>';
            previewStr += '<b>rakuten affi defaultyes</b> [Enter] とするとアフィリエイトが既定で有効になります<br/>';
            previewStr += '<b>rakuten affi defaultno</b> [Enter] とするとアフィリエイトが既定で無効になります<br/><br/>';
            previewStr += 'これらのアフィリエイトは私hokunarinの楽天でのアフィリエイトについて言っています。このコマンドをお使い頂く方でアフィ踏んでやってもいいという方は rakuten affi defaultyes [Enter] してみて頂けると嬉しいです<br/><br/>';
            previewStr += '※アフィリエイトが有効の場合、検索候補のプレビュー表示上段に <span class="affi" style="font-size:xx-small;border: 1px solid #440000; -moz-border-radius:2px; padding:0px 2px 0px 2px; overflow:visible;float:none;background:#eecccc;color:#662222;">Affiliate:ON</span> と表示されます<br/></div>';
            previewBlock.innerHTML = previewStr;
            return;
        }
        
        var order = mods["orderby"].text;
        var sortParam = "standard";
        var sortname = null;
        switch(order){
            case "price":
                sortParam = "+itemPrice";
                sortname = "価格昇順";
                break;
            case "-price":
                sortParam = "-itemPrice";
                sortname = "価格降順";
                break;
            case "date":
                sortParam = "+updateTimestamp";
                sortname = "商品更新日時昇順";
                break;
            case "-date":
                sortParam = "-updateTimestamp";
                sortname = "商品更新日時降順";
                break;
            default:
                sortParam = "standard";
        }
        
        
        var previewTemplate = '<b>${query}</b> を楽天市場で検索します...';
        var optionTemplate = '<span style="font-size:80%">{if sortname} 並べ替え:${sortname}{/if}{if !noaffi} <span class="affi">Affiliate:ON</span>{/if}</span>';
        var previewData = {query: searchText,
                            sortname: sortname,
                            noaffi: noaffi};
        previewBlock.innerHTML = CmdUtils.renderTemplate(previewTemplate, previewData);
        var optionHTML = CmdUtils.renderTemplate(optionTemplate, previewData);
        previewBlock.innerHTML += optionHTML;
        
        jQuery(previewBlock).append('<div style="position:absolute;top:40%;left:40%;width:100px;height:50px;" id="progressimage"><img src="http://ubiquity-commands-ja-nora.googlecode.com/files/image_7990.gif" /></div>').find("#progressimage").hide().fadeIn(2000);
        
        previewPage(0);
        
        function previewPage(pageDiff){
            
            var page = jQuery("#page", previewBlock).text();
            if(eval(page)>0){
                page = eval(page) + pageDiff;
            }else{
                page = 1;
            }
            
            if(ANIM){
                slideOut(jQuery(".item",previewBlock), pageDiff);
            }
            
            CmdUtils.previewAjax(previewBlock, {
                type: "GET",
                url: apiurl,
                data: {developerId: developerId,
                       affiliateId: affiliateId,
                       keyword: searchText,
                       operation: "ItemSearch",
                       version: "2009-04-15",
                       hits: 5,
                       page: page,
                       genreInformationFlag : 1,
                       sort : sortParam
                        },
                dataType: "json",
                
                error: function() {
                    previewBlock.innerHTML = "Error searching rakuten";
                },
                
                success: function(json) {
                    
                    var items = [];
                    var i = 0;
                    
                    var resultsnum = json.Body.ItemSearch.count;
                    
                    if(json.Body.ItemSearch.Items){
                        jQuery.each(json.Body.ItemSearch.Items.Item.slice(0,5), function(){
                            
                            var itemUrl = noaffi ? this.itemUrl : this.affiliateUrl;
                            
                            var price = (this.itemPrice + "").replace( /([0-9]+?)(?=(?:[0-9]{3})+$)/g , '$1,');
                            var shopUrl = noaffi ? this.shopUrl : affiliateUrlPrefix + encodeURIComponent(this.shopUrl);
                            
                            var imageUrl = this.smallImageUrl;
                            
                            var excludeTax = this.taxFlag;
                            var excludeShipping = this.postageFlag;
                            var canUseCreditCard = this.creditCardFlag;
                            
                            // smaller font size when length is over 90 
                            // 100% for length 90, 60% for length 200
                            var titleFontSize = (this.itemName.length>90) ? ( ( ( 60 - 100 ) / ( 200 - 90 ) ) * this.itemName.length + 100 - 90 * ( ( 60 - 100 ) / ( 200 - 90 ) ) ) : 100;
                            
                            var item = {key : ++i,
                                        itemname : this.itemName,
                                        itemnamesize: titleFontSize,
                                        code : this.itemCode,
                                        price : price,
                                        caption : this.itemCaption,
                                        shopName : this.shopName,
                                        shopUrl : shopUrl,
                                        url : itemUrl,
                                        image : imageUrl,
                                        excludeTax : excludeTax,
                                        excludeShipping : excludeShipping,
                                        canUseCreditCard : canUseCreditCard,
                                        reviewCount : this.reviewCount,
                                        reviewAverage : this.reviewAverage
                                        };
                            
                            items.push(item);
                            
                        });
                    }
                    
                    var genres = [];
                    var i = 5;
                    if(json.Body.ItemSearch.genreInformation.child){
                        
                        json.Body.ItemSearch.genreInformation.child.sort(function(a, b){
                            return b.itemCount - a.itemCount;
                        });
                        
                        jQuery.each(json.Body.ItemSearch.genreInformation.child.slice(0,2) , function(){
                            
                            var genreUrl = "http://esearch.rakuten.co.jp/rms/sd/esearch/vc?g=" + this.genreId;
                            genreUrl = noaffi ? genreUrl : affiliateUrlPrefix + encodeURIComponent(genreUrl);
                            
                            var genre = {   itemCount : this.itemCount,
                                            name : this.genreName,
                                            url : genreUrl,
                                            key : ++i
                                        };
                            
                            genres.push(genre);
                            
                        });
                    }
                    
                    var previewData = {
                        query : searchText,
                        resultsnum : resultsnum,
                        options: optionHTML,
                        items : items,
                        page : page,
                        genres : genres
                    };
                    
                    previewBlock.innerHTML = CmdUtils.renderTemplate( jQuery("#rakuten", feed.dom).html(), previewData);
                    
                    
                    if(page>1){
                        jQuery("#prev", previewBlock).show();
                        jQuery("#prev", previewBlock).one("click", function(){
                            previewPage( -1 );
                        });
                    }else{
                        jQuery("#prev", previewBlock).hide();
                    }
                    
                    if(items.length + 5*(page-1) < resultsnum ){
                        jQuery("#next", previewBlock).show();
                        jQuery("#next", previewBlock).one("click", function(){
                            previewPage( 1 );
                        });
                    }else{
                        jQuery("#next", previewBlock).hide();
                    }
                    
                    
                    //for Ubiquity 0.1.x / 0.5  difference
                    if (ubiquityVersion.indexOf("0.1.") > -1){
                        jQuery("#prev", previewBlock).attr({href:""});
                        jQuery("#next", previewBlock).attr({href:""});
                    }else{
                        jQuery("#prev", previewBlock).removeAttr("href");
                        jQuery("#next", previewBlock).removeAttr("href");
                    }
                    
                    
                    if(ANIM){
                        slideIn(jQuery(".item", previewBlock), pageDiff);
                    }
                    
                }
            });
        
        }
        
        function slideOut(items, pageDiff){
            var i=0;
            jQuery(items.get().reverse()).each(function(){
                var itemBlock = jQuery(this);
                var iw = itemBlock.width();
                var ih = itemBlock.height();
                var it = itemBlock.position().top;
                itemBlock.css({ "width": iw,
                                "height":ih,
                                "top":it,
                                "position":"absolute",
                                "opacity":"1"});
                itemBlock.animate( {"left": ((pageDiff>0)?"-":"+") + "=500px", "opacity":"0"}, 500 - pageDiff*(80 * i - 200) );
                i++;
            });
            
            jQuery(items).parent().append('<div style="position:absolute;top:40%;left:40%;width:100px;height:50px;" id="progressimage"><img src="http://ubiquity-commands-ja-nora.googlecode.com/files/image_7990.gif" /></div>').find("#progressimage").hide().fadeIn(2000);
            
        }
        
        function slideIn(items, pageDiff){
            
            //jQuery("#progressimage", items).remove();
            
            
            var i = 0;
            jQuery(items.get().reverse()).each(function(){
                var itemBlock = jQuery(this);
                var iw = itemBlock.width();
                var ih = itemBlock.height();
                var it = itemBlock.position().top;
                var il = itemBlock.position().left;
                itemBlock.css({ "width":iw,
                                "height":ih,
                                "top": it,
                                "left": ( eval( (pageDiff>=0?"+":"-") + "1") * 500 + il) + "px",
                                "position": "absolute",
                                "opacity":"0"});
                itemBlock.animate({"left":(pageDiff>=0?"-":"+") + "=500px", "opacity":"1"}, 500 - ((pageDiff==0)?1:pageDiff)*( i * 80 - 200 ) );
                i++;
            });
        }

        
    },

    execute: function( directObject, mods ) {
        
        var affiText = mods["affi"].text;
        if(affiText=="defaultyes"){
            Application.prefs.setValue("extensions.hokunarin.rakuten.affiliate", true);
            displayMessage("アフィリエイトが既定で有効になりました。");
            return;
        }else if(affiText=="defaultno"){
            Application.prefs.setValue("extensions.hokunarin.rakuten.affiliate", false);
            displayMessage("アフィリエイトが既定で無効になりました。");
            return;
        }
        
        var searchText = jQuery.trim(directObject.text);
        
        var affiText = mods["affi"].text;
        var noaffi = false;
        if(affiText && affiText.length > 1){
            if(affiText=="no"){
                noaffi = true;
            }
        }
        
        var order = mods["orderby"].text;
        var s = "0";
        switch(order){
            case "price":
                s = "2";
                break;
            case "-price":
                s = "1";
                break;
            case "-date":
                s = "5";
                break;
            default:
                s = "0";
        }
        
        var searchUrl = "http://esearch.rakuten.co.jp/rms/sd/esearch/vc?sv=2&s=" + s + "&sitem=" + escape( Utils.convertFromUnicode("euc-jp", searchText) );
        
        var url;
        
        if(noaffi){
            url = searchUrl;
        }else{
            url = affiliateUrlPrefix + encodeURIComponent(searchUrl);
        }
        
        Utils.openUrlInBrowser( url );
    }
    
});


]]></script>
</body>
</html>
